<?php

namespace app\index\controller;

use think\Db;

class Exam extends Base
{
    /**
     * 获取考试题目
     * $totalCount 总题目数量
     * $majorCount 专业题目数量
     * $commonCount 通用题目数量
     */
    public function getExam()
    {
        $where = [];
        $c_where = [];
        input('jobId') ? $where['jobId'] = input('jobId') : true;

        if(input('jobId')){
            $exam = config('exam');
            $totalCount = $exam['totalCount'];
            $majorCount = $exam['majorCount'];
            $commonCount = $totalCount - $majorCount;
            $res = model('exam')->getListByPage($where, $majorCount);
            $c_where['jobId'] = 0;
            $rep = model('exam')->getListByPage($c_where, $commonCount);
            $e_res = [
                'jobExam' => $res['data'],
                'jobExamCount' => $res['per_page'],
                'commonExam' => $rep['data'],
                'commonExamCount' => $rep['per_page']
            ];
        }else{
            $exam = config('exam');
            $totalCount = $exam['totalCount'];
            $commonCount = $totalCount;
            $c_where['jobId'] = 0;
            $rep = model('exam')->getListByPage($c_where, $commonCount);

            $e_res = [
                'commonExam' => $rep['data'],
                'commonExamCount' => $rep['per_page']
            ];
        }

        $e_res ? $this->suc($e_res) : $this->err('获取试题失败');
    }


    /**
     * 提交考试
     */
    public function pushExam()
    {

        input('cid') ? $cid = input('cid') : $this->err('缺少用户主键');
        input('totalCount') ? $totalCount = input('totalCount') : $this->err('缺少答题数量！');
        input('rightCount') == 0 || input('rightCount') ? $rightCount = input('rightCount') : $this->err('缺少正确答题数量！');
        input('useTime') ? $useTime = input('useTime') : $this->err('缺少消耗时间！');
        $score = 100 * $rightCount / $totalCount;
        $assignment['totalCount'] = $totalCount;
        $assignment['rightCount'] = $rightCount;
        $assignment['errorCount'] = $totalCount - $rightCount;
        $assignment['score'] = $score;
        $assignment['cid'] = input('cid');
        $assignment['createTime'] = createTime();
        $assignment['useTime'] = $useTime;
        //查询是否考试过
        $u_res = model('user')->where(['id' => $cid])->find();
        if ($u_res['examd']==1) {
            $this->err('该用户已经安全考试过！');
        }
        // 启动事务
        Db::startTrans();
        try {
            $res = model('user_exam')->editData($assignment);
            $res ? true : $this->err('失败');
            $data = [
                'examd' => 1,
                'id' => $cid
            ];
            //查询是否和魅力表有联系
            $safeExam = model('user_charm')->where(['id' => $cid])->find();

            $user = model('user')->editData($data, $data['id']);
            // 已经考试过就编辑用户魅力值表没有就新增一条数据
            if ($safeExam) {
                //编辑
                $charm_data = [
                    'cid' => $cid,
                    'safeExam' => $rightCount / $totalCount,
                    'safeExamPercent' => $score
                ];
                model('user_charm')->editData($charm_data, $charm_data['cid']);
            } else {
                // 新增
                $charm_data = [
                    'cid' => $cid,
                    'safeExam' => $rightCount / $totalCount,
                    'safeExamPercent' => $score
                ];
                  model('user_charm')->editData($charm_data);
            }
            $log_data = [
                'sign' =>1,
                'cid'=>$cid,
                'source' => 7,
                'score' => $score,
                'leftScore' => $u_res['charm'] + $score
            ];
            model('user_charm_log')->editData($log_data);
            // 提交事务
            if ($res) {
                Db::commit();
                $this->suc('提交成功！');
            } else {
                Db::rollback();
                $this->err('提交失败！');
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            //   $this->err($e);
            $this->err('操作失败！');
        }
    }

}
